package com.sgb.kjwl.view.ui.adapter.self.packers;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.SelfTeamOfferEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class SelfTeamOfferDeviceAdapter extends BaseRecycleAdapter<SelfTeamOfferEntity.EquipmentListDTO> {

    private RequestManager requestManager;

    public SelfTeamOfferDeviceAdapter(Context context, List<SelfTeamOfferEntity.EquipmentListDTO> data, RequestManager requestManager) {
        super(context, R.layout.item_team_offer_device, data);
        this.requestManager = requestManager;
    }

    @Override
    public void convert(ItemViewHolder holder, SelfTeamOfferEntity.EquipmentListDTO entity) {
        holder.setBinding(BR.viewModel, entity);
        ImageView ivDevice = holder.itemView.findViewById(R.id.iv_device);
        RequestOptions options = new RequestOptions();
        options.transforms(new CenterCrop());
        if (entity.getUrl() != null && !TextUtils.isEmpty(entity.getUrl())) {
            ivDevice.setVisibility(View.VISIBLE);
            requestManager.load(entity.getUrl()).apply(options).into(ivDevice);
        }else {
            ivDevice.setVisibility(View.GONE);
        }
        holder.itemView.findViewById(R.id.tv_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onEdit(entity, holder.getAdapterPosition());
            }
        });
        holder.itemView.findViewById(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onDelete(holder.getAdapterPosition());
            }
        });
    }

    public interface ClickResultListener {

        void onEdit(SelfTeamOfferEntity.EquipmentListDTO entity, int position);

        void onDelete(int position);
    }

    private ClickResultListener listener;

    public void setListener(ClickResultListener listener) {
        this.listener = listener;
    }
}
